#include "Hero.h"
#include "GameKey.h"


Hero::Hero() :m_blood(GAME_KEY_HERO_MAX_BLOOD), m_wudi(false)
{
}

Hero::~Hero()
{
}

bool Hero::init()
{
	if (!CCNode::init())
	{
		return false;
	}
	this->setAnchorPoint(ccp(0.5, 0.5));
	CCDirector* pDirector = CCDirector::sharedDirector();
	pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);

	CCSpriteFrame* planeFrame0 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("gra_planeImg0.png");
	CCSpriteFrame* planeFrame1 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("gra_planeImg1.png");
	CCSpriteFrame* planeFrame2 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("gra_planeImg2.png");


	CCSprite* body = CCSprite::createWithSpriteFrame(planeFrame0);
	CCSize size = body->getContentSize();
	this->setContentSize(size);
	body->setPosition(ccp(size.width / 2, size.height / 2));
	this->addChild(body);
	

	CCSprite* body1 = CCSprite::createWithSpriteFrame(planeFrame2);
	body1->setPosition(ccp(size.width / 2, size.height / 2));
	this->addChild(body1);

	return true;
}

bool Hero::inRect(CCPoint t)
{
	
	return this->boundingBox().containsPoint(t);
	/*CCSize size = this->getContentSize();
	return abs(this->getPosition().x - t.x) < size.width && abs(this->getPosition().y - t.y) < size.height;*/

}
bool Hero::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	if (!touch)
	{
		return false;
	}
	return inRect(touch->getLocation());
}
void Hero::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
	if (!touch)
	{
		return;
	}
	CCPoint t = touch->getLocation();
	CCPoint nowT = this->getPosition();
	CCSize size = this->getContentSize();
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	if ( t.x < size.width / 2  )
	{
		t.x = size.width / 2;
	}
	if ( t.y < size.height / 2 )
	{
		t.y = size.height / 2;
	}
	if ( t.x > (winSize.width - size.width/2))
	{
		t.x = winSize.width - size.width / 2;
	}
	if ( t.y > ( winSize.height - size.height/2))
	{
		t.y = winSize.height - size.height / 2;
	}
	this->setPosition(t);
}
void Hero::ccTouchEnded(CCTouch* touch, CCEvent* event)
{

}

void  Hero::setHurt()
{
	m_wudi = true;
	m_blood--;
	this->runAction(CCSequence::create(CCBlink::create(1.5f, 3), CCCallFuncN::create(this, callfuncN_selector(Hero::afterHurt)), NULL));
}
void  Hero::afterHurt(CCNode* pSender)
{
	this->setVisible(true);
	m_wudi = false;
}